package com.peng.utils;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

/**
 * 短信发送工具类
 */
public class SMSUtils {

	/**
	 * 发送短信
	 * @param signName 签名
	 * @param templateCode 模板
	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param) {
//		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "*", "*");
//		IAcsClient client = new DefaultAcsClient(profile);
//
//		SendSmsRequest request = new SendSmsRequest();
//		request.setSysRegionId("cn-hangzhou");
//		request.setPhoneNumbers(phoneNumbers);
//		request.setSignName(signName);
//		request.setTemplateCode(templateCode);
//		request.setTemplateParam("{\"code\":\"" + param + "\"}");
//		try {
//			SendSmsResponse response = client.getAcsResponse(request);
//			System.out.println("短信发送成功");
//		} catch (ClientException e) {
//			e.printStackTrace();
//		}
		String secretId="*";
		String secretKey="*";
		String appId="*";//实例化一个认证对象，入参需要传入腾讯云账户密钥对
		Credential cred = new Credential(secretId, secretKey);// 实例化一个 http 选项，可选，无特殊需求时可以跳过
		HttpProfile httpProfile = new HttpProfile();
		httpProfile.setReqMethod("POST");
		httpProfile.setConnTimeout(60);/* SDK 会自动指定域名，通常无需指定域名，但访问金融区的服务时必须手动指定域		 * 例如 SMS 的上海金融区域名为 sms.ap-shanghai-fsi.tencentcloudapi.com */
		httpProfile.setEndpoint("sms.tencentcloudapi.com");
		ClientProfile clientProfile = new ClientProfile();
		clientProfile.setSignMethod("HmacSHA256");
		clientProfile.setHttpProfile(httpProfile);/* 实例化 SMS 的 client 对		 * 第二个参数是地域信息，可以直接填写字符串 ap-guangzhou，或者引用预设的常量 */
		SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);//String[] param = {date};
		SendSmsRequest req = new SendSmsRequest();
		req.setSmsSdkAppid(appId);
		req.setTemplateID(templateCode);
		req.setSign(signName);
		String[] str={phoneNumbers};
		req.setPhoneNumberSet(str);
		String[] par={param};
		req.setTemplateParamSet(par);
		try {
			SendSmsResponse resp = client.SendSms(req);
			System.out.println(SendSmsResponse.toJsonString(resp));
		} catch (TencentCloudSDKException e) {
			e.printStackTrace();
		}
	}


//	private static String keyId = "*";
//	private static String regionld = "cn-hangzhou";
//	private static String secret = "*";
//	public static boolean SendSms(String PhoneNumbers,String TemplateParam) {
//		// 1.创建接口API调用对象
//		DefaultProfile profile = DefaultProfile.getProfile(regionld, keyId,secret);
//		IAcsClient client = new DefaultAcsClient(profile);
//		CommonRequest request = new CommonRequest();
//		// 2.发送post请求
//		request.setSysMethod(MethodType.POST);
//		request.setSysDomain("dysmsapi.aliyuncs.com"); //云服务(不能修改)
//		request.setSysVersion("2017-05-25"); //版本号(不能修改)
//		request.setSysAction("SendSms"); // 发送方式(不能修改)
//		request.putQueryParameter("RegionId", "cn-hangzhou");
//		// 发送的手机号（多个手机号可以用逗号隔开）
//		request.putQueryParameter("PhoneNumbers", PhoneNumbers);
//		request.putQueryParameter("SignName", "邓鹏个人学习使用");
//		// 3.请求模板(短信模板)
//		request.putQueryParameter("TemplateCode", "SMS_242706633");
//		// 4.请求模板参数（需要时json数组格式，验证码可以使用随机参数）
//		request.putQueryParameter("TemplateParam","{\"code\":" + TemplateParam + "}");
//		try {
//			CommonResponse response = client.getCommonResponse(request);
//			System.out.println(response.getData());
//			return true;
//		} catch (ServerException e) {
//			e.printStackTrace();
//		} catch (ClientException e) {
//			e.printStackTrace();
//		}
//		return false;
//
//	}


	public static void main(String[] args) {
//		SendSms("*","1231");
		sendMessage("*","*","*","1231");
	}
}
